import 'package:flutter/material.dart';

enum SearchBarTypeS { home, normal, homeLight }

class AppBarSearch extends StatefulWidget {
  final bool? enabledS;
  final bool? hideLeftS;
  final SearchBarTypeS? searchBarTypeS;
  final String? hintS;
  final String? defaultTextS;
  final void Function()? leftButtonOnClick;
  final void Function()? rightButtonOnClick;
  final void Function()? speakOnClick;
  final void Function()? inputBoxClick;
  final ValueChanged<String>? onTextChanged;

  AppBarSearch(
      {this.enabledS = true,
      this.hideLeftS,
      this.searchBarTypeS = SearchBarTypeS.home,
      this.hintS,
      this.defaultTextS,
      this.onTextChanged,
      this.leftButtonOnClick,
      this.rightButtonOnClick,
      this.speakOnClick,
      this.inputBoxClick});

  @override
  _AppBarSearchState createState() => _AppBarSearchState();
}

class _AppBarSearchState extends State<AppBarSearch> {
  bool showClear = false;
  final TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    if (widget.defaultTextS != null) {
      setState(() {
        _controller.text = widget.defaultTextS!;
      });
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return widget.searchBarTypeS == SearchBarTypeS.normal
        ? _getNormalSearchS()
        : getHomeSearchS();

  }

  // Container(
  // height: 60,
  // width: 110,
  // child: const ExpansionTile(
  // title: Text('上海'),children: [
  // ListTile(title: Text('北京',),),
  // ListTile(title: Text('天津'),),
  // ],
  // ))
  //

  getHomeSearchS() {
    return Row(
      children: [
        Row(
          children: [
            Text('上海',
              style: TextStyle(fontSize: 20, color: _color()),
            ),
            Icon(
              Icons.expand_more,
              color: _color(),
            )
          ],
        ),
        Expanded(flex: 1, child: _inputBox()),
        Container(
          padding:
              const EdgeInsets.only(left: 10, top: 5, right: 10, bottom: 5),
          child: Icon(
            Icons.comment,
            color: _color(),
            size: 26,
          ),
        ),
      ],
    );
  }

  _getNormalSearchS() {
    return Container(
        child: Row(children: [
      _wrapTapS(
          Container(
            padding: const EdgeInsets.only(left: 6, top: 10, right: 5),
            child: widget.hideLeftS!
                ? null
                : const Icon(
                    Icons.arrow_back_ios,
                    size: 25,
                  ),
          ),
          widget.leftButtonOnClick!),
      Expanded(flex: 1, child: _inputBox()),
      _wrapTapS(
          Container(
            padding: const EdgeInsets.only(left: 6, top: 10, right: 5),
            child: const Text(
              '搜索',
              style: TextStyle(color: Colors.blue, fontSize: 18),
            ),
          ),
          widget.rightButtonOnClick!)
    ]));
  }

  _wrapTapS(Widget child, void Function() callback) {
    return GestureDetector(
      onTap: () {
        if (callback != null) callback();
      },
      child: child,
    );
  }

  _color() {
    if (widget.searchBarTypeS == SearchBarTypeS.homeLight) {
      return Colors.black45;
    } else {
      return Colors.white;
    }
  }

  _inputBox() {
    Color inputBoxColor;
    if (widget.searchBarTypeS == SearchBarTypeS.normal) {
      inputBoxColor = Colors.black12;
    } else {
      inputBoxColor = Color(int.parse('0xffEDEDED'));
    }
    return Container(
      height: 40,
      decoration: BoxDecoration(
          color: inputBoxColor,
          borderRadius: BorderRadius.circular(
              widget.searchBarTypeS == SearchBarTypeS.normal ? 6 : 14)),
      child: Row(
        children: [
          Icon(
            Icons.search,
            size: 19,
            color: widget.searchBarTypeS == SearchBarTypeS.normal
                ? const Color(0xffA9A9A8)
                : Colors.blue,
          ),
          Expanded(
              flex: 1,
              child: widget.searchBarTypeS == SearchBarTypeS.normal
                  ? TextField(
                      controller: _controller,
                      autofocus: true,
                      style: const TextStyle(
                        fontSize: 17,
                        fontWeight: FontWeight.w300,
                      ),
                      decoration: InputDecoration(
                          contentPadding: const EdgeInsets.only(
                            left: 5,
                            right: 5,
                          ),
                          hintText: widget.hintS ?? "",
                          hintStyle: const TextStyle(fontSize: 17),
                          border: InputBorder.none),
                      onChanged: _onChanged,
                    )
                  : _wrapTapS(
                      Text(
                        widget.defaultTextS!,
                        style: const TextStyle(fontSize:17),
                      ),
                      widget.inputBoxClick!)),
          !showClear
              ? _wrapTapS(
                  Icon(
                    Icons.mic,
                    size: 20,
                    color: widget.searchBarTypeS == SearchBarTypeS.home
                        ? Colors.black45
                        : Colors.blue,
                  ),
                  widget.speakOnClick!)
              : _wrapTapS(
                  const Icon(Icons.clear, size: 20, color: Colors.black45), () {
                  setState(() {
                    _controller.clear();
                  });
                  _onChanged('')!;
                })
        ],
      ),
    );
  }

  _onChanged(String text) {
    setState(() {
      if (text.length > 0) {
        showClear = true;
      } else {
        showClear = false;
      }
    });
    if (widget.onTextChanged != null) {
      widget.onTextChanged!(text);
    }
  }
}
